<template>
  <div class="login">
    <van-nav-bar @click-left="$router.back()" title="账号登录" left-arrow> </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button type="primary" native-type="submit" block>登录</van-button>
      </div>
    </van-form>
    <a href="javascript:;">还没有账号点击去登录</a>
  </div>
</template>

<script>
import { goLogin } from '@/api/login.js'
import { Toast } from 'vant'

export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },

  created () {},

  methods: {
    async onSubmit () {
      try {
        const res = await goLogin(this.form)
        console.log(res)
        if (res.status === 200) {
          this.$router.push('/layout/my')
          this.$store.commit('initUser', res.data.body)
          Toast.success('登录成功')
        }
      } catch (err) {
        // console.log(err)
        Toast.fail('登录失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login {
  .van-nav-bar {
    background-color: #21b97a;
    /deep/ .van-nav-bar__title {
      color: #fff;
    }
    /deep/ .van-icon {
      color: #fff;
    }
  }
  .van-cell {
    height: 80px;
    width: 100%;
    padding: 30px 20px;
  }
  a {
    display: inline-block;
    color: #666;
    width: 100%;
    text-align: center;
    font-size: 30px;
  }
}
</style>
